import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.List;

public class JedisDemoZset {
    public static void test1(Jedis jedis) {
        //有序集合
        System.out.println("zadd 和 zrange");
        jedis.flushAll();
        jedis.zadd("key",100,"zhangsan");
        List<Tuple> tupleList = jedis.zrangeWithScores("key", 0, -1);
        System.out.println(tupleList);
    }
    public static void test2(Jedis jedis) {
        System.out.println("zcard的使用");
        jedis.flushAll();
        jedis.zadd("key",100,"zhangsan");
        jedis.zadd("key",101,"lisi");
        jedis.zadd("key",102,"wangwu");
        Long len=jedis.zcard("key");
        System.out.println("长度为: "+len);
    }
    public static void test3(Jedis jedis) {
        System.out.println("zrem的使用");
        jedis.flushAll();
        jedis.zadd("key",100,"zhangsan");
        jedis.zadd("key",101,"lisi");
        jedis.zadd("key",102,"wangwu");
        Long len=jedis.zrem("key","wangwu");
        System.out.println("删除的长度为："+len);
        List<Tuple> tupleList = jedis.zrangeWithScores("key", 0, -1);
        System.out.println(tupleList);
        //根据分数进行遍历
        List<String> strings = jedis.zrangeByScore("key", 100,102);
        System.out.println(strings);
    }
    public static void test4(Jedis jedis) {
        System.out.println("zrank的使用");
        jedis.flushAll();
        jedis.zadd("key",100,"zhangsan");
        jedis.zadd("key",101,"lisi");
        jedis.zadd("key",102,"wangwu");
        //这里返回的结果为这个元素的下标
        Long zranked = jedis.zrank("key", "lisi");
        System.out.println("排名为："+zranked);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://localhost:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.auth("maihao1122");
            //测试类
            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            test4(jedis);
        }
    }
}
